﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Drawing.Printing;

namespace CochinitaCorte
{
    public partial class printDialog : Form
    {
        private MainForm.FormReturn ReturnedValues;
        private Settings settings;

        public printDialog(MainForm.FormReturn value, object _settings)
        {
            ReturnedValues = value;
            this.settings = (Settings)_settings;
            InitializeComponent();
        }

        private void printDialog_Load(object sender, EventArgs e)
        {
            foreach (string printer in PrinterSettings.InstalledPrinters)
                PrintersList.Items.Add(printer);

            SetLabels(settings.PrinterName);
        }

        private void SetLabels(string printername)
        {
            lblStatus.Text = StatusPrinter(printername);
            lblPrinter.Text = printername;
        }

        private bool CheckPrinter(string printername)
        {
            try
            {
                PrintDocument print = new PrintDocument();
                print.PrinterSettings.PrinterName = printername;
                return print.PrinterSettings.IsValid;
            }
            catch
            {
                return false;
            }
        }

        private string StatusPrinter(string printername)
        {
            return ((CheckPrinter(printername)) ? "Lista" : " Apagada");
        }

        private void PrintersList_SelectedIndexChanged(object sender, EventArgs e)
        {
            SetLabels(PrintersList.SelectedItem.ToString());
          
            BusinessLayer BL = new BusinessLayer(false);
            Settings ST = new Settings();
            
            ST.PrinterName = PrintersList.SelectedItem.ToString();
            
            if (BL.SetSettings(ST))
                MessageBox.Show("Datos Actualizados", "Actualizacion con Exito!!", MessageBoxButtons.OK, MessageBoxIcon.Information);
           
        }

        private void btnPrint_Click(object sender, EventArgs e)
        {
            ReturnedValues.Invoke("IMPRIMIR",(rdbZ.Checked ?"CORTE_Z": (rdbCajeros.Checked ? "CORTE_C" : (rbnComplete.Checked ? "CORTE_COMPLETE" : "CORTE_N"))));
        }

        private void rdbNormal_CheckedChanged(object sender, EventArgs e)
        {

        }
    }
}
